package com.etoak.controller;


import com.etoak.common.core.vo.PageVO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Dishes;
import com.etoak.entity.OrderItem;
import com.etoak.entity.vo.DishesVO;
import com.etoak.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author et2406
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/dishes")
public class DishesController {

    @Autowired
    private DishesService dishesService;

    @PostMapping
    public ResultVO<Object> addDishes(@Validated @RequestBody Dishes dishes) {
        dishesService.addDishes(dishes);
        return ResultVO.success();
    }

    /**
     * get /dishes/list
     *
     * @param pageNum  当前页
     * @param pageSize 一页大小
     * @param dishes   模糊查询参数
     * @return pageVo
     */
    @GetMapping("/list")
    public ResultVO<PageVO> getList(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            Dishes dishes
    ) {
        PageVO<DishesVO> list = dishesService.getList(pageNum, pageSize, dishes);
        return ResultVO.success(list);
    }

    /**
     * 更新菜品
     * put /dishes/{id}
     */
    @PutMapping("/{id}")
    public ResultVO<Object> update(
            @PathVariable int id,
            @RequestBody Dishes dishes
    ){
        dishesService.update(id,dishes);
        return ResultVO.success();
    }

    /**
     * 删除菜品 delete /dishes/{id}
     */
    @DeleteMapping("/{id}")
    public ResultVO<Object> delete(@PathVariable int id){
        dishesService.delete(id);
        return ResultVO.success();
    }


    @PostMapping("/list")
    public ResultVO<List<DishesVO>> getDishesByDishesId(@RequestBody List<OrderItem> orderItemList){
        List<DishesVO> dishesByDishesId = dishesService.getDishesByDishesId(orderItemList);
        return ResultVO.success(dishesByDishesId);
    }


}

